遊戲在昨天已經告一段落了,那我們在最後一天來Review這30天來主要學到哪些東西吧!
transform.Translate(x軸上的位移量,y軸上的位移量,z軸上的位移量);
Input.GetKey (KeyCode key);
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;//要使用TextMeshPro資料型態時需引入TMPro
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour
{
[SerializeField] float speed = 5f;
GameObject currentFloor;
[SerializeField] int Hp;
[SerializeField] GameObject HpBar;
[SerializeField] TextMeshProUGUI scoreText;
int score;//紀錄現在到第幾level
float scoreTime;//紀錄現在過了多久時間
AudioSource deathSound;
[SerializeField]GameObject replayButton;
void Start()
{
Hp = 10;
score = 0;
scoreTime = 0;
deathSound = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKey(KeyCode.D)) {
transform.Translate(speed*Time.deltaTime,0,0);
GetComponent<SpriteRenderer>().flipX = false;
GetComponent<Animator>().SetBool("run",true);
}
else if(Input.GetKey(KeyCode.A)) {
transform.Translate(-speed*Time.deltaTime,0,0);
GetComponent<SpriteRenderer>().flipX = true;
GetComponent<Animator>().SetBool("run",true);
}
else
{
GetComponent<Animator>().SetBool("run",false);
}
UpdateScore();
}
void OnCollisionEnter2D(Collision2D other) //other是指碰撞到的東西
{
if(other.gameObject.tag == "Normal")
{
if(other.contacts[0].normal == new Vector2(0f,1f))
{
Debug.Log("撞到Normal");
currentFloor = other.gameObject;
ModifyHp(1);
other.gameObject. GetComponent<AudioSource>().Play();
}
}
else if(other.gameObject.tag == "Nails")
{
if(other.contacts[0].normal == new Vector2(0f,1f))
{
Debug.Log("撞到Nails");
currentFloor = other.gameObject;
ModifyHp(-3);
GetComponent<Animator>().SetTrigger("hurt");
other.gameObject. GetComponent<AudioSource>().Play();
}
}
else if(other.gameObject.tag == "Ceiling")
{
Debug.Log("撞到天花板");
currentFloor.GetComponent<BoxCollider2D>().enabled = false;
ModifyHp(-3);
GetComponent<Animator>().SetTrigger("hurt");
other.gameObject.GetComponent<AudioSource>().Play();
}
}
void OnTriggerEnter2D(Collider2D other) {
if(other.gameObject.tag == "DeathLine")
{
Debug.Log("你輸了!");
deathSound.Play();
Time.timeScale = 0f;
replayButton.SetActive(true);
}
}
void ModifyHp(int num)
{
Hp += num;
if(Hp > 10)
{
Hp = 10;
}
else if(Hp <= 0)
{
Hp = 0;
deathSound.Play();
Time.timeScale = 0f;
replayButton.SetActive(true);
}
UpdateHpBar();
}
void UpdateHpBar()
{
for(int i=0;i<HpBar.transform.childCount;i++)
{
if(Hp>i)
{
HpBar.transform.GetChild(i).gameObject.SetActive(true);
}
else
{
HpBar.transform.GetChild(i).gameObject.SetActive(false);
}
}
}
void UpdateScore()
{
scoreTime +=Time.deltaTime;//用Time.deltaTime來計算時間,因為它是每次Update方法被呼叫到的間隔時間
if(scoreTime>2f) //只要scoreTime超過2秒,就把分數+1更新到文字上,並歸零時間
{
score++;
scoreTime = 0f;
scoreText.text = "Level" + score.ToString();
}
}
public void Replay()
{
Time.timeScale = 1f;
SceneManager.LoadScene("SampleScene");
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Floor : MonoBehaviour
{
[SerializeField] float moveSpeed = 2f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
transform.Translate(0,moveSpeed*Time.deltaTime,0);
if(transform.position.y > 6f)
{
Destroy(gameObject);
transform.parent.GetComponent<FloorManager>().SpawnFloor();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FloorManager : MonoBehaviour
{
[SerializeField] GameObject[] FloorPrefabs;
public void SpawnFloor()
{
int r = Random.Range(0,FloorPrefabs.Length);
GameObject floor = Instantiate(FloorPrefabs[r],transform);
floor.transform.position = new Vector3(Random.Range(-3.8f,3.8f),-6f,0f);
}
}
終於要從今年的鐵人賽畢業啦~雖然30天說長不長說短不短,但寫IT已經變成我這過去30天的習慣,每天有空先做的事情就是寫文章,也從這裡學習到很多Unity的技術
最後再次由衷感謝影片教程的作者,解說得很好、很用心,大家也一定要去觀看這個影片喔!
參考網址:https://www.youtube.com/watch?v=nPW6tKeapsM&ab_channel=GrandmaCan-%E6%88%91%E9%98%BF%E5%AC%A4%E9%83%BD%E6%9C%83
音效、圖片 : 遊戲素材
(素材由安德斯提供,感謝大大)